$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/barticlestand/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', align: "center", width: 50, key: true},
            {label: '文章ID', name: 'articleId', index: 'article_id', align: "center", width: 80},
            {label: '文章标题', name: 'articleTitle', index: 'article_title', align: "center", width: 80},
            {label: '开始时间', name: 'startTime', index: 'start_time', align: "center", width: 80},
            {label: '结束时间', name: 'endTime', index: 'end_time', align: "center", width: 80},
            {label: '文章分类', name: 'articleTypeName', index: 'article_type_name', align: "center", width: 80},
            {
                label: '资源类型',
                name: 'resourceType',
                index: 'resource_type',
                align: "center",
                width: 80,
                formatter: function (value, options, rowData) {
                    let content = "";
                    if (value === 1) {
                        content = '文章';
                    } else if (value === 2) {
                        content = "视频"
                    } else if (value === 3) {
                        content = "素材"
                    }
                    return content;
                }
            },
            // {label: '排序', name: 'orderInx', index: 'order_inx', align: "center", width: 80},
            // {label: '', name: 'createTime', index: 'create_time', align: "center", width: 80},
        ],
        viewrecords: true,
        height: 520,
        rowNum: 15,
        rowList: [15, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        bArticleStand: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.bArticleStand = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.bArticleStand.id == null ? "sys/barticlestand/save" : "sys/barticlestand/update";

            let timeStart = new Date(vm.bArticleStand.startTime).getTime();
            let timeEnd = new Date(vm.bArticleStand.endTime).getTime();

            if (!(timeStart > 0) || !(timeEnd > 0)) {
                alert('请选择正确的推送时间');
                return;
            }

            if (timeStart > timeEnd ) {
                alert('开始时间不能大于结束时间');
                return;
            }

            vm.bArticleStand.startTime = timeStart;
            vm.bArticleStand.endTime = timeEnd;

            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.bArticleStand),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/barticlestand/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "sys/barticlestand/info/" + id, function (r) {
                vm.bArticleStand = r.bArticleStand;

                vm.bArticleStand.startTime = timeSet(r.bArticleStand.startTime);
                vm.bArticleStand.endTime = timeSet(r.bArticleStand.endTime);

                function timeSet(time) {
                    let myDate = new Date(time)
                    let curTime = myDate.getFullYear() + "-" + fix((myDate.getMonth() + 1), 2) + "-" + fix(myDate.getDate(), 2) + "T" + fix(myDate.getHours(),
                        2) + ":" + fix(myDate.getMinutes(), 2);
                    return curTime;
                }

                //将日期格式化为两位，不足补零
                function fix(num, length) {
                    return ('' + num).length < length ? ((new Array(length + 1)).join('0') + num).slice(-length) : '' + num;
                }


                vm.bArticleStand.startTime.replace(" ", "T")
                vm.bArticleStand.endTime.replace(" ", "T")
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        }
    }
});
